package cn.kgc.goodsdemo.controller;

import cn.kgc.goodsdemo.pojo.Goodsdetail;
import cn.kgc.goodsdemo.pojo.ResultEntity;
import cn.kgc.goodsdemo.service.GoodsdetailService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class DetailController {

    @Resource
    GoodsdetailService goodsdetailService;
    @RequestMapping("/seledetail")
    public ResultEntity<PageInfo<Goodsdetail>> seledetail(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                                          @RequestParam(value = "sortid",required = false,defaultValue = "0")Integer sortid){
        PageInfo<Goodsdetail> pageInfo = goodsdetailService.seleDetail(pageNum, sortid);
    return ResultEntity.successWithoutData(pageInfo);
    }
    @RequestMapping("/seleById")
    public ResultEntity<Goodsdetail> seleById(Integer id){
        Goodsdetail goodsdetail = goodsdetailService.seleById(id);
        System.out.println("数据"+goodsdetail);
    return ResultEntity.successWithoutData(goodsdetail);
    }
    @RequestMapping("/up")
    public ResultEntity<String> up(@RequestBody Goodsdetail goodsdetail,Integer count){
       Integer newCount=goodsdetail.getRemaining()-count;
       goodsdetail.setRemaining(newCount);
    try {
        Integer up = goodsdetailService.up(goodsdetail);
        return ResultEntity.successWithoutData(up+"");
    }catch(Exception ex){
        return ResultEntity.failed(ex.getMessage());
    }


    }
}
